package com.iwhale.sequence.redisidx;

import com.iwhale.sequence.redisidx.config.RedisConfig;
import com.iwhale.sequence.redisidx.config.SequenceConfig;
import com.iwhale.sequence.redisidx.sequence.SequenceServer;
import com.iwhale.sequence.redisidx.utils.ApplicationContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;


@SpringBootApplication
@EnableConfigurationProperties({RedisConfig.class, SequenceConfig.class})
public class SeqApplication implements CommandLineRunner {
    private static Logger logger = LoggerFactory.getLogger(SeqApplication.class);

    @Autowired
    private SequenceServer sequenceServer;

    public static void main(String[] args) {
        SpringApplication app = new SpringApplication(SeqApplication.class);
        app.addListeners((ApplicationListener<ContextRefreshedEvent>) event -> {
            ApplicationContextUtil.setApplicationContext(event.getApplicationContext());
        });
        app.run(args);
    }

    /**
     * 启动服务
     *
     * @param args 
     */
    public void run(String... args) {
        sequenceServer.start();
    }
}